//
//  WBComposeView.swift
//  SinaWeibo
//
//  Created by 风不会停息 on 2016/11/21.
//  Copyright © 2016年 风不会停息. All rights reserved.
//

import UIKit
import pop
class WBComposeView: UIView {

    
    lazy var buttonArray: [WBComposeButton] = [WBComposeButton]()
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
    }
    
    func show(fromVC: UIViewController?) {
        //将当前的视图添加到window上/根视图控制器的view上/传递控制器
        fromVC?.view.addSubview(self)
        //开始动画
        for (index,btn) in buttonArray.enumerated() {
            self.startAnimation(btn: btn, index: index)
        }
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    private func setupUI() {
        //添加被处理毛玻璃效果的背景视图  截取屏幕
        let backView = UIImageView(image: UIImage.snapShotCurrentWindow())
        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
        toolBar.barStyle = .blackTranslucent
//        self.addSubview(backView)
        self.addSubview(toolBar)
        self.addSubview(sloganView)
        
        sloganView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        addComposeButtons()
    }
    private func addComposeButtons() {
        
        let btnMargin = (ScreenWidth - 3 * composeBtnImageWidth) / 4
        
        for i in 0..<6 {
            let btn = WBComposeButton()
            //应该确定对应的列的索引
            let coloumIndex = i % 3
            let rowIndex = i / 3
            
            let btnX = btnMargin + (composeBtnImageWidth + btnMargin) * CGFloat(coloumIndex)
            //计算 y
            let btnY = (composeBtnHeight + btnMargin) * CGFloat(rowIndex) + ScreenHeight
            
            switch i {
            case 0:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_idea"), for: .normal)
                btn.setTitle("@好友", for: .normal)
            case 1:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_photo"), for: .normal)
                btn.setTitle("@照片/视频", for: .normal)
            case 2:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_wbcamera"), for: .normal)
                btn.setTitle("@头条文章", for: .normal)
            case 3:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_lbs"), for: .normal)
                btn.setTitle("@签到", for: .normal)
            case 4:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_shooting"), for: .normal)
                btn.setTitle("@直播", for: .normal)
            case 5:
                btn.setImage(#imageLiteral(resourceName: "tabbar_compose_more"), for: .normal)
                btn.setTitle("@更多", for: .normal)
            default:
                break
            }
            
//            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_friend"), for: .normal)
//            btn.setTitle("@好友", for: .normal)
            btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnImageWidth, height: composeBtnHeight)
//            btn.backgroundColor = UIColor.red
            btn.addTarget(self, action: #selector(composeBtnClick(btn:)), for: .touchUpInside)
            self.addSubview(btn)
            buttonArray.append(btn)
        }
    }

    
//    private lazy var backView:UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyDarkEffect())
    
    private lazy var sloganView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
    //点击移除模糊效果的图片
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (index,btn) in buttonArray.reversed().enumerated() {
            self.startAnimation(btn: btn, index: index,isShow: false)
        }
        
        //动画执行完毕之后 移除视图
        //延迟移除
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {

            self.removeFromSuperview()
        })
    }
    private func startAnimation(btn:WBComposeButton,index:Int,isShow:Bool = true){
        /*
         POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
         
         CGPoint point = [tap locationInView:self.view];
         
         springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)];
         
         
         //弹性值
         springAnimation.springBounciness = 20.0;
         //弹性速度
         springAnimation.springSpeed = 20.0;
         
         [_springView pop_addAnimation:springAnimation forKey:@"changeframe"];
         */
        //实例化弹性动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        springAnimation?.springBounciness = 10.0
        springAnimation?.springSpeed = 10
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350:350)
        springAnimation?.toValue = NSValue.init(cgPoint:CGPoint(x: btn.center.x, y: btnCenterY))
        btn.pop_add(springAnimation, forKey: nil)
        
    }
    @objc private func composeBtnClick(btn:WBComposeButton){
        
        UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0
            for composeBtn in self.buttonArray{
                if composeBtn == btn{
                    //就是被点击的按钮 放大\
                    composeBtn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                }else{
                    composeBtn.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                    composeBtn.alpha = 0.1
                }
            }
        }, completion: { (_) -> () in
            //2.跳转对应的控制器
            //通过网络解析得到要跳转的控制器的名称
            let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
            let className = bundleName + "." + "WBComposeViewController"
            guard let classType = NSClassFromString(className) as? UIViewController.Type else{
                return
            }
            let vc = classType.init()
            let nav = UINavigationController(rootViewController: vc)
            UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: {
                self.removeFromSuperview()
            })
            
        })
    }
    

}
